package com.ccsoft.leetcode.service;

import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;

/**
 * 13. 罗马数字转整数
 */
@Service
public class RomanNumerals {
    public int romanToInt(String s) {
        Map<String,Integer> stardardMap = getMap();
        Integer[] vals = new Integer[s.length()];
        for(int i=0;i<s.length();i++){
            // 当前位序字符串
            char c = s.charAt(i);            Integer val = stardardMap.get(String.valueOf(c));

            // 右边字符串
            int j = i+1;
            if( j<s.length() ){
                char cRight = s.charAt(j);
                Integer valRight = stardardMap.get(String.valueOf(cRight));
                if( val < valRight ) val = val * -1;
            }

            vals[i] = val;
        }

        Integer ret = 0;
        for(Integer i:vals){
            ret += i;
        }
        return ret.intValue();
    }

    public Map<String,Integer> getMap(){
        Map<String,Integer> map = new HashMap<>();
        map.put("I",1);
        map.put("V",5);
        map.put("X",10);
        map.put("L",50);
        map.put("C",100);
        map.put("D",500);
        map.put("M",1000);

        return map;
    }
}
